#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/7/8 15:59
# @File    : canvas_app.py
# @Description : -i http://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn

print("___start___")
import tkinter as tk
import ttkbootstrap as ttk




print("___plot___")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.patches import Rectangle

class RectangleDrawer:
    def __init__(self, master):
        self.master = master
        self.fig, self.ax = self.create_figure()
        self.canvas = FigureCanvasTkAgg(self.fig, master=master)
        self.canvas_widget = self.canvas.get_tk_widget()
        self.canvas_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

        self.rect = None
        self.rect_props = {'xs': None, 'ys': None, 'xe': None, 'ye': None}

        self.canvas.mpl_connect('button_press_event', self.on_button_press)
        self.canvas.mpl_connect('button_release_event', self.on_button_release)
        self.canvas.mpl_connect('motion_notify_event', self.on_motion_notify)

        self.master.protocol("WM_DELETE_WINDOW", self.on_closing)
        self.show()

    def create_figure(self):
        fig = Figure(figsize=(5, 4), dpi=100)
        ax = fig.add_subplot(111)
        ax.set_xlim(0, 10)
        ax.set_ylim(0, 10)
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        ax.grid(True)
        return fig, ax

    def show(self):
        self.canvas.draw()

    def on_button_press(self, event):
        if event.inaxes is None:
            return
        self.rect_props['xs'] = event.xdata
        self.rect_props['ys'] = event.ydata
        facecolor = 'lightgray'
        self.rect = Rectangle((event.xdata, event.ydata), 0, 0, edgecolor='r', facecolor=facecolor)
        self.ax.add_patch(self.rect)
        self.canvas.draw_idle()

    def on_motion_notify(self, event):
        if event.inaxes is None or self.rect is None:
            return
        self.rect.set_width(event.xdata - self.rect_props['xs'])
        self.rect.set_height(event.ydata - self.rect_props['ys'])
        self.canvas.draw_idle()

    def on_button_release(self, event):
        if event.inaxes is None or self.rect is None:
            return
        self.canvas.draw_idle()

    def on_closing(self):
        self.master.destroy()

if __name__ == '__main__':
    print("___init___")

    try:

        #from prophet import Prophet
        # print(Prophet())
        # import pandas
        # import matplotlib
        # import numpy

        from sklearn import linear_model
        from lightgbm import LGBMRegressor

    except Exception as e:
        print("error")

    print("___tk___")
    root = ttk.Window()
    app = RectangleDrawer(root)
    root.mainloop()